<html>
<head><meta charset="utf-8"><title>Idiomatic rust flags · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomatic.20rust.20flags.html">Idiomatic rust flags</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242873812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomatic%20rust%20flags/near/242873812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomatic.20rust.20flags.html#242873812">(Jun 16 2021 at 12:38)</a>:</h4>
<p>Do we have a idiomatic way of maintaining flags?<br>
enums are great for anything that's mutually exclusive, but it isn't great for things that can aggregate,<br>
so you either do:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">Flags</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">One</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Two</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Three</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">OneTwo</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">OneThree</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Note that Two and Three are mutually exclusive</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>another option is to seperate the flags into multiple enums and a struct, but the memory representation of that is currently not optimial (<a href="https://play.rust-lang.org/?gist=1650ccb51fa1311b86f2678aaf9206f4">https://play.rust-lang.org/?gist=1650ccb51fa1311b86f2678aaf9206f4</a>)<br>
This still allows mutually exclusive flags (you can only choose a single write flag in my example)</p>
<p>The last option is crates like <code>bitflags</code> but I have 2 problems with those:</p>
<ol>
<li>It doesn't allow mutually exclusive flags.</li>
<li>It exposes an implementation detail that I don't care about (choosing sizes and bits).</li>
</ol>
<p>The 2nd one is the important problem IMHO, rust doesn't have layout guarantees exactly because of that, to allow great optimizations depending on the usage and on niche.</p>
<p>Would love to hear thoughts, either on other ways to do this or on ideas for new language features to allow this</p>



<a name="242876480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomatic%20rust%20flags/near/242876480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomatic.20rust.20flags.html#242876480">(Jun 16 2021 at 13:02)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> your "struct and enums" idea seems good, it sounds like the only drawback was the memory layout? That could be improved without language changes.</p>



<a name="242878512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomatic%20rust%20flags/near/242878512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomatic.20rust.20flags.html#242878512">(Jun 16 2021 at 13:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Idiomatic.20rust.20flags/near/242876480">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> your "struct and enums" idea seems good, it sounds like the only drawback was the memory layout? That could be improved without language changes.</p>
</blockquote>
<p>The main downside of that approach is that it requires writing a lot of boilerplate code, and using the result is also not too nice. (altough with complex match expressions you might be able to do some cool things)</p>



<a name="242914535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomatic%20rust%20flags/near/242914535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomatic.20rust.20flags.html#242914535">(Jun 16 2021 at 17:29)</a>:</h4>
<p>Looks like the compiler just does it as a newtype over an integer, and some constants: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ReprFlags.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ReprFlags.html</a></p>
<p>As for the language change, it'd probably be this: <a href="https://internals.rust-lang.org/t/towards-even-smaller-structs/14686/3?u=scottmcm">https://internals.rust-lang.org/t/towards-even-smaller-structs/14686/3?u=scottmcm</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>